package cn.itsource.org.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.domain.ShopLog;
import cn.itsource.org.domain.vo.ShopVo;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopLogMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.service.IShopService;
import cn.itsource.utils.BaiduAuditUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.List;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService{

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopLogMapper shopLogMapper;

    @Autowired
    private JavaMailSender mailSender;

    @Override
    @Transactional
    public void settlement(Shop shop) throws Exception {
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new Exception("店铺名称不合法，请重新输入！");
        }
        Employee employee = shop.getAdmin();
        //保存 店铺信息和 管理员信息
        //设置数据的状态
        shop.setState(0);
        employee.setState(0);
        //设置入驻时间
        shop.setRegisterTime(new Date());
      //System.out.println("保存之前："+shop.getAdmin().getId());

        //需要开启 employee保存数据的时候的 返回主键的功能
        employeeMapper.save(employee);
      //System.out.println("保存之后："+shop.getAdmin().getId());
        //admin 对象里面 就由id
        super.save(shop);

    }

    @Override
    public void reject(ShopLog shopLog) {
        //1.把店铺的状态设置为 -1
        Shop shop = shopMapper.loadById(shopLog.getShop_id());
        shop.setState(-1);
        shopMapper.update(shop);
        //2.记录日志
        //一下两句代码 以后再改
        Employee employee = new Employee();
        employee.setId(1L);
        shopLog.setEmployee(employee);
        shopLogMapper.save(shopLog);

        //店铺关联的管理员
        Employee manager = employeeMapper.loadById(shop.getAdmin_id());
        //3.发送邮件通知 商家 审核失败，告知原因，修改的地址
        //复杂邮件对象
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            //使用工具类来构建消息
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            //
            helper.setFrom("64009120@qq.com");
            //设置邮件主题
            helper.setSubject("审核失败通知");
            //邮件正文
            helper.setText("<h1>审核失败</h1><br>"+
                    "<span>失败原因："+shopLog.getNote()+"</span><br>"+
                    "<a href='http://127.0.0.1:8081/#/register2?id="+shop.getId()+"'>点击修改资料</a>",true);
            helper.setTo(manager.getEmail());
            //发送消息
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {

            e.printStackTrace();
        }

    }

    @Override
    public Shop loadByAdminId(Long id) {
        return shopMapper.loadByAdminId(id);
    }

    @Override
    public List<ShopVo> countByState() {
        return shopMapper.countByState();
    }
}
